package notesDetection.GUI;

//	http://www.java2s.com/Code/Java/Swing-JFC/DemonstrationofFiledialogboxes.htm

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class FileBrowser extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1928732644322753890L;

	private JTextField filename = new JTextField();
	private JTextField dir = new JTextField();

	private JButton open = new JButton("Open");
	private JButton save = new JButton("Save");

	public FileBrowser() {
	    JPanel p = new JPanel();
	    open.addActionListener(new OpenFile());
	    p.add(open);
	    save.addActionListener(new SaveFile());
	    p.add(save);
	    Container cp = getContentPane();
	    cp.add(p, BorderLayout.SOUTH);
	    dir.setEditable(false);
	    filename.setEditable(false);
	    p = new JPanel();
	    p.setLayout(new GridLayout(2, 1));
	    p.add(filename);
	    p.add(dir);
	    cp.add(p, BorderLayout.NORTH);
	}

  class OpenFile implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      JFileChooser fileChooser = new JFileChooser();
      
      try
      {
    	  File currentLocation = new File(dir.getText());
    	  fileChooser.setCurrentDirectory(currentLocation);
      }
      catch(Exception error)
      {
    	  //	An error has occurred. Don't set anything. 
      }
      //	"Open" dialog:
      int rVal = fileChooser.showOpenDialog(FileBrowser.this);
      if (rVal == JFileChooser.APPROVE_OPTION) {
        filename.setText(fileChooser.getSelectedFile().getName());
        dir.setText(fileChooser.getCurrentDirectory().toString());
      }
      if (rVal == JFileChooser.CANCEL_OPTION) {
        filename.setText("You pressed cancel");
        dir.setText("");
      }
    }
  }

  class SaveFile implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      JFileChooser fileChooser = new JFileChooser();
      //	"Save" dialog:
      int rVal = fileChooser.showSaveDialog(FileBrowser.this);
      if (rVal == JFileChooser.APPROVE_OPTION) {
        filename.setText(fileChooser.getSelectedFile().getName());
        dir.setText(fileChooser.getCurrentDirectory().toString());
      }
      if (rVal == JFileChooser.CANCEL_OPTION) {
        filename.setText("You pressed cancel");
        dir.setText("");
      }
    }
  }

  public void HideWindow()
  {
	  setVisible(false);
  }
  
  public String getSelectedFilePath()
  {
	  String currentFile = dir.getText() + '\\' + filename.getText(); 
	  
	  return (new File(currentFile)).exists() ? currentFile : null;
	  
  }
  
  public void run(int width, int height) {
    //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(width, height);
    setVisible(true);
  }
}
